using System;
using BruteForce.NewManagers;
using BruteForce.UserInterface.Abstracts;
using BruteForce.Utilities;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BruteForce.UserInterface.UIControls
{
    delegate Boolean DelegateBooleanFunction();
    delegate int DelegateIntFunction();

    public class Slider : AbstractUIControl
    {
        private int _maxValue;
        private int _minValue;

        private readonly Texture2D _sliderBackground;
        private readonly Texture2D _sliderScrollBar;
        private readonly Color _valueTextColor;
        private int _sliderValue;
        private float _sliderXpostion;
        private Rectangle _shorterCurrentUIcontrolRectangle;


        private DelegateBooleanFunction _booleanFunction;
        private DelegateIntFunction _intFunction;

        public Slider(Texture2D sliderBackgroundImage, Texture2D sliderScrollBarImage, string name,
                      Rectangle surroundingRectangle, int minVal, Color valueColor, Func<Boolean> booleanMethod,Func<int> intMethod)
            : base(name, surroundingRectangle)
        {
            _minValue = minVal;
            _maxValue = minVal;
            _sliderXpostion = 0;
            _sliderBackground = sliderBackgroundImage;
            _sliderScrollBar = sliderScrollBarImage;
            _sliderValue = minVal;
            _sliderXpostion = surroundingRectangle.X;
            _valueTextColor = valueColor;
            _booleanFunction = new DelegateBooleanFunction(booleanMethod);
            _intFunction = new DelegateIntFunction(intMethod);
            _shorterCurrentUIcontrolRectangle = new Rectangle(CurrentUIcontrolRectangle.X, CurrentUIcontrolRectangle.Y, CurrentUIcontrolRectangle.Width - 15, CurrentUIcontrolRectangle.Height);
        }

        public int GetSliderValue
        {
            get { return _sliderValue; }
        }

        public override void Update()
        {
            if (_booleanFunction())
            {
                _maxValue = _intFunction() + 1;
                if ((InputManager.Instance.MouseClicked.IsChangedSince(BFGame.LoopIndex) &&
                     InputManager.Instance.IsMouseClickedOn(_shorterCurrentUIcontrolRectangle)))
                {
                    float relativeSliderXpostion = InputManager.Instance.CurMouseX - _shorterCurrentUIcontrolRectangle.X;
                    relativeSliderXpostion = relativeSliderXpostion / _shorterCurrentUIcontrolRectangle.Width;
                    _sliderValue = (int) (relativeSliderXpostion*(_maxValue - _minValue));
                    if (_sliderValue < _minValue)
                        _sliderValue = _minValue;
                    _sliderXpostion = InputManager.Instance.CurMouseX;
                    if (_sliderXpostion > RelativeUIcontrolRectangle.Width + _shorterCurrentUIcontrolRectangle.X)
                    {
                        _sliderXpostion = RelativeUIcontrolRectangle.Width + _shorterCurrentUIcontrolRectangle.X;
                    }
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if (_booleanFunction())
            {
                BFGame.SpriteBatch.Draw(_sliderBackground,
                                        new Rectangle(CurrentUIcontrolRectangle.X, CurrentUIcontrolRectangle.Y,
                                                      RelativeUIcontrolRectangle.Width,
                                                      RelativeUIcontrolRectangle.Height),
                                        Color.White);
                BFGame.SpriteBatch.Draw(_sliderScrollBar,
                                        new Rectangle((int) _sliderXpostion, CurrentUIcontrolRectangle.Y - 6, 20,
                                                      _sliderScrollBar.Height), Color.White);
                Draws.DrawString((int) _sliderXpostion, CurrentUIcontrolRectangle.Y, "" + _sliderValue, _valueTextColor);
                //CurrentUIcontrolRectangle.X + CurrentUIcontrolRectangle.Width + 5
            }
        }

        public void Reset()
        {
            _sliderValue = _minValue;
            _sliderXpostion = RelativeUIcontrolRectangle.X;
            
        }

        public void SetMinValue(int i)
        {
            _minValue = i;
            _sliderValue = i;
        }
    }
}